package com.media.publish.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.baselibrary.utils.KeyBoardUtils;
import com.baselibrary.utils.SpacesRightDec;
import com.liaoinstan.springview.utils.DensityUtil;
import com.media.R;
import com.qiniu.pili.droid.shortvideo.PLMediaFile;
import com.xinlan.imageeditlibrary.editimage.model.TextColorListEnum;

import java.util.List;
import java.util.Objects;

import com.media.publish.adapter.QNTextColorSelectListAdapter;

/**
 * 编辑模块文字特效添加的视图
 */
public class TextBottomView extends StickerBottomView {
    private OnTextSelectorListener mOnTextSelectorListener;
    private QNTextColorSelectListAdapter mQnVideoTextListAdapter;
    private TextColorListEnum mTextColorBean;

    private List<StickerTextView> mProceedStickerTextViews;
    private StickerTextView mProceedStickerTextView;
    private TextColorListEnum[] values;

    public void setProceedStickerTextViews(List<StickerTextView> mProceedStickerTextViews) {
        this.mProceedStickerTextViews = mProceedStickerTextViews;
    }

    public void setProceedStickerTextView(StickerTextView mProceedStickerTextView) {
        this.mProceedStickerTextView = mProceedStickerTextView;
    }

    public interface OnTextSelectorListener {
        /**
         * 新增文字特效时触发
         *
         * @param textView
         */
        void onTextAdded(StickerTextView textView, long startTimeMs, long durationMs);

        /**
         * 文字特效被选择时触发
         *
         * @param textView
         */
        void onTextSelected(StickerTextView textView);

        /**
         * 编辑文字特效时触发
         *
         * @return
         */
        boolean onTextEdited(StickerTextView mStickerText);

        /**
         * 删除文字特效时触发
         *
         * @return
         */
        boolean onTextDeleted();

        /**
         * 文字特效作用时间时间改变时触发
         *
         * @param textView
         * @param startTimeMs
         * @param durationMs
         */
        void onTextRangeChanged(StickerTextView textView, long startTimeMs, long durationMs);
    }

    public TextBottomView(@NonNull Context context, AppCompatActivity act, PLMediaFile mediaFile, LayoutInflater mInflater) {
        super(context, act, mediaFile, mInflater);
    }

    public void setOnTextSelectorListener(OnTextSelectorListener listener) {
        mOnTextSelectorListener = listener;
    }

    public void setEditText(String text) {
        if (mEdtVideoAddTextEdit != null)
            mEdtVideoAddTextEdit.setText(text);
    }

    @Override
    protected void init() {
        super.init();

        values = TextColorListEnum.values();
        mTextColorBean = values[0];
        mEdtVideoAddTextEdit.setTextColor(mTextColorBean.getParseColor());//默认颜色为红色
        mStickerRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mStickerRecyclerView.addItemDecoration(new SpacesRightDec(DensityUtil.dip2px(Objects.requireNonNull(getContext()), 20)));
        mQnVideoTextListAdapter = new QNTextColorSelectListAdapter(mInflater, getContext());
        mQnVideoTextListAdapter.setList(values);
        mStickerRecyclerView.setAdapter(mQnVideoTextListAdapter);
        mQnVideoTextListAdapter.setOnTextColorClickListener(textColorListBean -> {
            mTextColorBean = textColorListBean;
            mEdtVideoAddTextEdit.setTextColor(mTextColorBean.getParseColor());
        });

        mTvVideoAddTextConfirm.setOnClickListener(v -> {

            StickerTextView mStickerText = null;
            if (mOnViewOperateListener != null) {
                mOnViewOperateListener.onConfirmClicked();
            }

            if (!TextUtils.isEmpty(mEdtVideoAddTextEdit.getText().toString())) {
                if (mProceedStickerTextViews != null && mProceedStickerTextView != null && mProceedStickerTextViews.contains(mProceedStickerTextView)) {
                    mStickerText = mProceedStickerTextView;
                    mStickerText.setEditable(true);
                    mStickerText.setText(mEdtVideoAddTextEdit.getText().toString());
                    mStickerText.setTextColor(mTextColorBean.getParseColor());
                    mStickerText.setFocusable(true);
                    mStickerText.requestFocus();
                    if (mOnTextSelectorListener != null)//添加文字
                        mOnTextSelectorListener.onTextRangeChanged(mStickerText, 0, 0);
                } else {
                    mStickerText = (StickerTextView) inflate(mContext, R.layout.sticker_text_view, null);
                    mStickerText.showEdit(true);
                    mStickerText.setText(mEdtVideoAddTextEdit.getText().toString());
                    mStickerText.setTextColor(mTextColorBean.getParseColor());
                    stickerClick(mStickerText);
                    if (mOnTextSelectorListener != null)//添加文字
                        mOnTextSelectorListener.onTextAdded(mStickerText, 0, 0);
                }

            }
        });
    }

    public void initStateView() {
        values = TextColorListEnum.values();
        mTextColorBean = values[0];
        mQnVideoTextListAdapter.setList(values);
        mQnVideoTextListAdapter.setClearStateView();
        mQnVideoTextListAdapter.notifyDataSetChanged();
        mEdtVideoAddTextEdit.setTextColor(mTextColorBean.getParseColor());
    }


    //文字的监听
    private void stickerClick(StickerTextView mStickerText) {
        mStickerText.setOnStickerOperateListener(new OnStickerOperateListener() {
            @Override
            public void onDeleteClicked() {
                if (mOnTextSelectorListener != null) {
                    mOnTextSelectorListener.onTextDeleted();
                }
            }

            @Override
            public void onEditClicked() {
                if (mOnTextSelectorListener != null) {
                    mOnTextSelectorListener.onTextEdited(mStickerText);
                }
            }

            @Override
            public void onRotateClicked() {

            }

            @Override
            public void onStickerSelected() {
                if (mOnTextSelectorListener != null) {
                    mOnTextSelectorListener.onTextSelected(mStickerText);
                }
            }
        });
    }

    public void keyboardListener(Activity activity) {
        RelativeLayout.LayoutParams mOccupyLayoutParams = (RelativeLayout.LayoutParams) mViewOccupy.getLayoutParams();
        KeyBoardUtils.setListener(activity, new KeyBoardUtils.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                mOccupyLayoutParams.height = height;
                mViewOccupy.setLayoutParams(mOccupyLayoutParams);
            }

            @Override
            public void keyBoardHide(int height) {
                mOccupyLayoutParams.height = 0;
                mViewOccupy.setLayoutParams(mOccupyLayoutParams);
            }
        });
    }


    @Override
    public boolean isPlayerNeedZoom() {
        return false;
    }

    @Override
    protected void playStatusChanged(boolean isPlaying) {
    }

}
